<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2017/7/14
  Time: 11:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";

%>
<html>
<head>
    <jsp:include page="../js_top.jsp"/>
    <link href="<%=basePath%>/static/css/css.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" href="<%=basePath%>/static/loginpage/css/style.css" />
    <title>登录页面</title>
    <style type="text/css">
        html, body {
            height: 100%;
        }
    </style>
</head>
<body>

<div id="login" class="login-container">
    <h1>医院挂号管理系统</h1>
    <form action="" method="post" id="loginForm">
        <div>
            <input type="text" name="account" id="loginName" class="loginName" placeholder="用户名" autocomplete="off"/>
        </div>
        <div>
            <input type="password" name="password" id="password" class="password" placeholder="密码" oncontextmenu="return false" onpaste="return false" />
        </div>
        <div>
            <em></em>
            <input type="text" name="securityCode" id="securityCode" class="loginName"  placeholder="验证码" autocomplete="off" onclick="createCode()"/>
            <input type="button" name="securityCode2" id="securityCode2" class="loginName" onClick="createCode()" />
        </div>
        <button id="submit" onclick="login();">登 录</button>
    </form>
    <a href="/toregister.do?">
        <button type="button" class="register-tis">还没有账号？</button>
    </a>
</div>

<script src="<%=basePath%>/static/loginpage/js/jquery.min.js"></script>
<script src="<%=basePath%>/static/loginpage/js/common.js"></script>
<!--背景图片自动更换-->
<script src="<%=basePath%>/static/loginpage/js/supersized.3.2.7.min.js"></script>
<script src="<%=basePath%>/static/loginpage/js/supersized-init.js"></script>
<!--表单验证-->
<script src="<%=basePath%>/static/loginpage/js/jquery.validate.min.js?var1.14.0"></script>
</body>
<script type="text/javascript">
    /*敲Enter键时登录*/
    document.onkeydown=function(event){
        var e = event || window.event;
        if(e && e.key == 13){
            login();
        }
    }
    /*登录*/
    function login(){
        if(check()&&validate()){
            var loginName = $("#loginName").val();
            var password = $("#password").val();
            var url = "<%=basePath%>sys/sysUser/login.do";
            var postData = {
                "loginName":loginName,
                "password":password,
            };
            $.post(url,postData,function(data){
                if(data && data=="success"){
                    $.ajax({
                        cache: true,
                        type: "POST",
                        url: '<%=basePath%>sys/sysRole/findUserRoles.do',
                        data: "",
                        async: false,
                        error: function (request) {
                            layer.msg('<span style="color:white;">获取角色出错 !</span>');
                        },
                        success: function (data) {
                            if(data){
                                var json = eval("("+data+")");
                                var role = "" ;
                                $.each(json,function(i,item){

                                    if(item.roleId == "1"){
                                        role = "admin";
                                    }
                                    if(item.roleId == "2"){
                                        role = "admin";
                                    }
                                    if(item.roleId == "4"){
                                        role = "doctor";
                                    }
                                });

                                if(role == "admin" || role == "doctor"){
                                    parent.location.href = "<%=basePath%>sys/sysUser/main.do";
                                }else{
                                    parent.location.href = "<%=basePath%>tofrontloginedmain.do";
                                }

                            }
                        }

                    });
                }else{
                    $("#loginName").tips({
                        side : 1,
                        msg : "用户名或密码有误",
                        bg : '#FF5080',
                        time : 3
                    });
                    $("#loginName").focus();
                    createCode();
                }
            });

        }

    }
    /*验证账号密码不为空*/
    function check(){
        if($("#loginName").val() == null || $("#loginName").val() == ""){
            $("#loginName").tips({
                side : 2,
                msg : '用户名不得为空',
                bg : '#AE81FF',
                time : 3
            });
            $("#loginName").focus();
            return false;
        }else{
            $("#loginName").val(jQuery.trim($("#loginName").val()));
        }
        if($("#password").val() == null || $("#password").val() == ""){
            $("#password").tips({
                side : 2,
                msg : '密码不得为空',
                bg : '#AE81FF',
                time : 3
            });
            $("#password").focus();
            return false;
        }else{
            $("#password").val(jQuery.trim($("#password").val()));
        }
        return true;
    }

    /*创建验证码*/
    var code;
    function createCode(){
        code = new Array();
        var codeLength = 4;
        var selectChar = new Array(2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');
        for(var i=0;i<codeLength;i++){
            code += selectChar[Math.floor(Math.random()*32)];
        }
        $("#securityCode2").val(code);
    }

    /*验证验证码正确*/
    function validate(){
        var inputCode = $("#securityCode").val().toUpperCase();
        if(inputCode == null || inputCode == ""){
            $("#securityCode").tips({
                side : 2,
                msg : '验证码不得为空',
                bg : '#AE81FF',
                time : 3
            });
            $("#securityCode").focus();
            return false;
        }else if(inputCode != code){
            $("#securityCode").tips({
                side : 2,
                msg : '验证码错误',
                bg : '#AE81FF',
                time : 3
            });
            $("#securityCode").focus();
            return false;
        }else{
            return true;
        }
    }
    $(document).keypress(function(e) {
        // 回车键事件
        if(e.which == 13) {
            login();
        }
    });

</script>
</html>
